home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / pctchnqs / 1991 / number3 / keybuff.c < prev    next >
Text File  |  1991-04-05  |  4KB  |  121 lines

  1. /* KEYBUFF.C -- Keyboard Buffer Stuffer Demo
  2.    For "PC Techniques" by Shawn D. Wolf      */
  3.  
  4. /* REMEMBER:
  5.    This source uses the 80x86-specific (non-ANSI) "far"
  6.    keyword (supported by both Turbo C/C++ and Microsoft C
  7.    in their "native" modes).  DO NOT compile the source in
  8.    an "ANSI source" mode.
  9.  
  10.    ALSO: Do not run the .EXE from the Borland IDE. (It will
  11.    "type" into the IDE windows!)  Shell out to DOS and run
  12.    it from the DOS prompt. */
  13.  
  14. /* ------- function prototypes ------- */
  15. void clear_key_buff(void);
  16. int key_buff_space(void);
  17. int put_char_in_buff(unsigned char bchar,int extend);
  18. int put_string_in_buff(char *str_ptr);
  19.  
  20. #include <dos.h>
  21.  
  22. /* ------- global pointers ------- */
  23. /* keyboard buffer head: */
  24. static int far *bufferhead = (int far *) 0x41A;
  25. /* keyboard buffer tail: */
  26. static int far *buffertail = (int far *) 0x41C;
  27. /* keyboard buffer: */
  28. static char far *keybuffer = (char far *) 0x41E;
  29.  
  30. /* ------- start of code ------- */
  31. /* This listing for Turbo C. For Microsoft C, change disable() to
  32.    _disable() and enable() to _enable(). */
  33.  
  34. main ()
  35.    {
  36.    clear_key_buff();
  37.    put_string_in_buff("DIR\x0D");
  38.  
  39.    /* Now let's put an 'F3' to repeat last command. */
  40.  
  41.    put_char_in_buff('\x3D',1);    /* Put 'F3' into buffer. */
  42.    put_char_in_buff('\x0D',0);    /* Put 'RETURN' into buffer. */
  43.  
  44.    return 0;
  45.    }
  46.  
  47.  
  48. void clear_key_buff(void)
  49.    /* clears the keyboard buffer by setting the tail = head */
  50.    {
  51.    disable();     /* turn off the normal keyboard interrupt */
  52.    *buffertail = *bufferhead;
  53.    enable();               /* restore the normal interrupts */
  54.    }
  55.  
  56.  
  57. int key_buff_space(void)
  58.    /* Returns the number of free keypress spaces in the buffer.
  59.       (not bytes.) Returns 15 if buffer is empty, 0 if full. */
  60.    {
  61.    return (15 - ((*buffertail - *bufferhead) / 2)) % 16;
  62.    }
  63.  
  64.  
  65. int put_char_in_buff(unsigned char bchar,int extend)
  66.    /* Places passed character into keyboard buffer. If extend is
  67.       true, treats keypress as extended and places it into
  68.       second byte.  Otherwise, places it into first byte. */
  69.    {
  70.    unsigned char first_byte,second_byte;
  71.  
  72.    if (!key_buff_space())      /* Is the keyboard buffer full? */
  73.       return 0;                /* If so, return error. */
  74.  
  75.    if (extend)                 /* Is this an extended keypress? */
  76.       {                        /* Yes, it is extended. */
  77.       first_byte = 0;          /* Zero goes in first byte. */
  78.       second_byte = bchar;     /* Passed char goes into second. */
  79.       }
  80.    else                              /* No, it is not extended. */
  81.       {
  82.       first_byte = bchar;  /* Passed char goes into first byte. */
  83.       second_byte = '/';   /* Dummy scan code goes into second. */
  84.       }
  85.  
  86.    /* Place first byte: */
  87.    *(keybuffer + (*buffertail - 0x1E)) = first_byte;
  88.    /* Increment the tail - no need to check for wrap: */
  89.    ++(*buffertail);
  90.  
  91.    /* Place 2nd byte: */
  92.    *(keybuffer + (*buffertail - 0x1E)) = second_byte;
  93.    if (++(*buffertail) > 0x3D) /* Increment tail - check for wrap. */
  94.       *buffertail = 0x1E;      /* If wrap, set tail to
  95.                                   lowest buffer memory. */
  96.  
  97.    return 1;
  98.    }
  99.  
  100.  
  101. int put_string_in_buff(char *string_ptr)
  102.    /* Takes passed string and places it into keyboard buffer.
  103.       Returns true on success, false on failure. */
  104.    {
  105.    int rval;
  106.  
  107.    rval = 1;
  108.    disable();                  /* Don't interrupt this string. */
  109.    while (*string_ptr)         /* Loop thru each char in string.  */
  110.       /* Put char into buffer: */
  111.       if (!put_char_in_buff(*string_ptr++,0))
  112.          {             /* We're assuming it's a normal keypress.  */
  113.          rval = 0;     /* If placement not successful, exit loop. */
  114.          break;
  115.          }
  116.  
  117.    enable();                         /* Restore interrupts. */
  118.  
  119.    return rval;                      /* Return success status. */
  120.    }
  121.